import React, { Component } from 'react'
import Far from './Far'
import PubSub from 'pubsub-js'
export default class App extends Component {
  render() {
    // 这是订阅的代码
    // 返回值就是当前订阅的唯一标识符.用来清除订阅
    const subId = PubSub.subscribe('money', (topic, data) => {
      console.log('app', topic, data)
    })
    const xxxId = PubSub.subscribe('xxx', (topic, data) => {
      console.log('app', topic, data)
    })
    return (
      <div>
        App
        <hr />
        <button
          onClick={() => {
            //取消订阅
            PubSub.unsubscribe(subId) //清除指定订阅
          }}
        >
          清除App的money订阅
        </button>
        <button
          onClick={() => {
            //取消订阅
            PubSub.unsubscribe('money') //清除当前话题的所有订阅
          }}
        >
          清除money话题的所有订阅
        </button>
        <button
          onClick={() => {
            //取消订阅
            PubSub.clearAllSubscriptions() //无差别的清除所有的订阅
          }}
        >
          清除所有订阅
        </button>
        <hr />
        <Far></Far>
      </div>
    )
  }
}
